<template>
    <div class="app-container">
        <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
            <el-form-item label="" prop="studentCourseId">
                <el-input
                        v-model="queryParams.studentCourseId"
                        placeholder="请输入"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="订单" prop="orderId">
                <el-input
                        v-model="queryParams.orderId"
                        placeholder="请输入订单"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="购买课程订单" prop="orderDetailId">
                <el-input
                        v-model="queryParams.orderDetailId"
                        placeholder="请输入购买课程订单"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="购买课时数量" prop="totalHour">
                <el-input
                        v-model="queryParams.totalHour"
                        placeholder="请输入购买课时数量"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="剩余课时数量" prop="balanceHour">
                <el-input
                        v-model="queryParams.balanceHour"
                        placeholder="请输入剩余课时数量"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="购买天数" prop="totalDay">
                <el-input
                        v-model="queryParams.totalDay"
                        placeholder="请输入购买天数"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="开始时间 按时间收费" prop="beginDate">
                <el-date-picker clearable size="small" style="width: 200px"
                                v-model="queryParams.beginDate"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="选择开始时间 按时间收费">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="结束时间 按时间收费" prop="endDate">
                <el-date-picker clearable size="small" style="width: 200px"
                                v-model="queryParams.endDate"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="选择结束时间 按时间收费">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="失效时间" prop="expireDate">
                <el-date-picker clearable size="small" style="width: 200px"
                                v-model="queryParams.expireDate"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="选择失效时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="总金额" prop="totalFee">
                <el-input
                        v-model="queryParams.totalFee"
                        placeholder="请输入总金额"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="单价" prop="unitFee">
                <el-input
                        v-model="queryParams.unitFee"
                        placeholder="请输入单价"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="有效 1:有效 0:作废" prop="valid">
                <el-input
                        v-model="queryParams.valid"
                        placeholder="请输入有效 1:有效 0:作废"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                        v-hasPermi="['sc:student:add']"
                >新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        icon="el-icon-edit"
                        size="mini"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['sc:student:update']"
                >修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="danger"
                        icon="el-icon-delete"
                        size="mini"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['sc:student:delete']"
                >删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        icon="el-icon-download"
                        size="mini"
                        @click="handleExport"
                        v-hasPermi="['::export']"
                >导出</el-button>
            </el-col>
        </el-row>

        <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column prop="studentCourseId" label=""/>
            <el-table-column prop="orderId" label="订单"/>
            <el-table-column prop="orderDetailId" label="购买课程订单"/>
            <el-table-column prop="totalHour" label="购买课时数量"/>
            <el-table-column prop="balanceHour" label="剩余课时数量"/>
            <el-table-column prop="totalDay" label="购买天数"/>
            <el-table-column label="开始时间 按时间收费" align="center" prop="beginDate" width="200">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.beginDate) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="结束时间 按时间收费" align="center" prop="endDate" width="200">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.endDate) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="失效时间" align="center" prop="expireDate" width="200">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.expireDate) }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="totalFee" label="总金额"/>
            <el-table-column prop="unitFee" label="单价"/>
            <el-table-column prop="valid" label="有效 1:有效 0:作废"/>
            <el-table-column width="150" label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                            v-hasPermi="['sc:student:update']"
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                    >修改</el-button>
                    <el-button
                            v-hasPermi="['sc:student:delete']"
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                    >删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
        />

        <!-- 添加或修改对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="600px">
            <el-form v-loading="loadingChange" ref="form" :model="form" :rules="rules" label-width="80px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label=":" prop="studentCourseId">
                            <el-input v-model="form.studentCourseId" placeholder="请输入" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="订单:" prop="orderId">
                            <el-input v-model="form.orderId" placeholder="请输入订单" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="购买课程订单:" prop="orderDetailId">
                            <el-input v-model="form.orderDetailId" placeholder="请输入购买课程订单" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="购买课时数量:" prop="totalHour">
                            <el-input v-model="form.totalHour" placeholder="请输入购买课时数量" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="剩余课时数量:" prop="balanceHour">
                            <el-input v-model="form.balanceHour" placeholder="请输入剩余课时数量" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="购买天数:" prop="totalDay">
                            <el-input v-model="form.totalDay" placeholder="请输入购买天数" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="开始时间 按时间收费:" prop="beginDate">
                            <el-date-picker clearable size="small" style="width: 200px"
                                            v-model="form.beginDate"
                                            type="date"
                                            value-format="yyyy-MM-dd"
                                            placeholder="选择开始时间 按时间收费">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="结束时间 按时间收费:" prop="endDate">
                            <el-date-picker clearable size="small" style="width: 200px"
                                            v-model="form.endDate"
                                            type="date"
                                            value-format="yyyy-MM-dd"
                                            placeholder="选择结束时间 按时间收费">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="失效时间:" prop="expireDate">
                            <el-date-picker clearable size="small" style="width: 200px"
                                            v-model="form.expireDate"
                                            type="date"
                                            value-format="yyyy-MM-dd"
                                            placeholder="选择失效时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="总金额:" prop="totalFee">
                            <el-input v-model="form.totalFee" placeholder="请输入总金额" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="单价:" prop="unitFee">
                            <el-input v-model="form.unitFee" placeholder="请输入单价" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="有效 1:有效 0:作废:" prop="valid">
                            <el-input v-model="form.valid" placeholder="请输入有效 1:有效 0:作废" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="创建者:" prop="createUser">
                            <el-input v-model="form.createUser" placeholder="请输入创建者" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="创建时间:" prop="createTime">
                            <el-date-picker clearable size="small" style="width: 200px"
                                            v-model="form.createTime"
                                            type="date"
                                            value-format="yyyy-MM-dd"
                                            placeholder="选择创建时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="更新者:" prop="lastUpdateUser">
                            <el-input v-model="form.lastUpdateUser" placeholder="请输入更新者" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="更新时间:" prop="lastUpdateTime">
                            <el-date-picker clearable size="small" style="width: 200px"
                                            v-model="form.lastUpdateTime"
                                            type="date"
                                            value-format="yyyy-MM-dd"
                                            placeholder="选择更新时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button :loading="loadingChange" type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import { listStudent, getStudent, delStudent, addStudent, updateStudent } from '@/api/sc/student'

    export default {
        name: 'Student',
        data() {
            return {
                // 遮罩层
                loading: true,
                loadingChange: false,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 总条数
                total: 0,
                // 表格树数据
                dataList: [],
                // 弹出层标题
                title: '',
                // 是否显示弹出层
                open: false,
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    studentCourseId: undefined,
                    orderId: undefined,
                    orderDetailId: undefined,
                    totalHour: undefined,
                    balanceHour: undefined,
                    totalDay: undefined,
                    beginDate: undefined,
                    endDate: undefined,
                    expireDate: undefined,
                    totalFee: undefined,
                    unitFee: undefined,
                    valid: undefined,
                },
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                    studentCourseId: [
                        { required: true, message: '不能为空', trigger: 'blur' }
                    ],
                    orderId: [
                        { required: true, message: '订单不能为空', trigger: 'blur' }
                    ],
                    orderDetailId: [
                        { required: true, message: '购买课程订单不能为空', trigger: 'blur' }
                    ],
                }
            }
        },
        created() {
            this.getList()
        },
        methods: {
            getList() {
                this.loading = true
                listStudent(this.queryParams).then(response => {
                    this.dataList = response.data.rows
                    this.total = response.data.total
                    this.loading = false
                })
            },
            // 取消按钮
            cancel() {
                this.open = false
                this.reset()
            },
            // 表单重置
            reset() {
                this.form = {
                    studentCourseId: undefined,
                    orderId: undefined,
                    orderDetailId: undefined,
                    totalHour: undefined,
                    balanceHour: undefined,
                    totalDay: undefined,
                    beginDate: undefined,
                    endDate: undefined,
                    expireDate: undefined,
                    totalFee: undefined,
                    unitFee: undefined,
                    valid: undefined,
                    createUser: undefined,
                    createTime: undefined,
                    lastUpdateUser: undefined,
                    lastUpdateTime: undefined,
                }
                this.resetForm('form')
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1
                this.getList()
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm('queryForm')
                this.handleQuery()
            },
            /** 新增按钮操作 */
            handleAdd(row) {
                this.reset()
                this.open = true
                this.title = '添加学生课程关联订单'
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.courseOrderId)
                this.single = selection.length !== 1
                this.multiple = !selection.length
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset()
                getStudent(row.courseOrderId || this.ids).then(response => {
                    this.form = response.data
                    this.open = true
                    this.title = '修改学生课程关联订单'
                })
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loadingChange = true
                        if (this.form.courseOrderId !== undefined) {
                            updateStudent(this.form).then(response => {
                                this.loadingChange = false
                                if (response.respCode === '0000') {
                                    this.msgSuccess('修改成功')
                                    this.open = false
                                    this.getList()
                                } else {
                                    this.msgError(response.respMsg)
                                }
                            }).catch(() => {
                                this.loadingChange = false
                            })
                        } else {
                            addStudent(this.form).then(response => {
                                this.loadingChange = false
                                if (response.respCode === '0000') {
                                    this.msgSuccess('新增成功')
                                    this.open = false
                                    this.getList()
                                } else {
                                    this.msgError(response.respMsg)
                                }
                            }).catch(() => {
                                this.loadingChange = false
                            })
                        }
                    }
                })
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const id = row.courseOrderId || this.ids
                this.$confirm('是否确认删除?', '警告', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function() {
                    return delStudent(id)
                }).then((response) => {
                    if (response.respCode === '0000') {
                        this.getList()
                        this.msgSuccess('删除成功')
                    } else {
                        this.msgError(response.respMsg)
                    }
                }).catch(function() {})
            }
        }
    }
</script>
